# -*- coding: utf-8 -*-
import scrapy
from scrapy.spiders import Rule, CrawlSpider
from scrapy.linkextractors import LinkExtractor

from scrapy_51job.items import Scrapy51JobItem

from scrapy_51job.utils import list_get

import re


class Job51Spider(CrawlSpider):
    name = 'job51'
    allowed_domains = ['51job.com']
    start_urls = ['https://search.51job.com/list/080200,000000,0000,00,9,99,%2520,2,1.html']

    rules = (
        Rule(LinkExtractor(allow=r"https://jobs.51job.com/.*/\d+.html"), callback="parse_item", follow=True),
        Rule(LinkExtractor(restrict_xpaths="//div[@class=\"p_in\"]/ul/li[@class=\"bk\"][2]/a"), follow=True),
    )

    def parse_item(self, response):
        infos = response.xpath("/html/body/div[3]/div[2]/div[2]/div/div[1]/p[2]/@title").extract_first()
        job_name = response.xpath("/html/body/div[3]/div[2]/div[2]/div/div[1]/h1/text()").extract_first()
        job_url = response.request.url
        company_name = response.xpath("/html/body/div[3]/div[2]/div[2]/div/div[1]/p[1]/a[1]/text()").extract_first()
        employee_welfare = ",".join(
            response.xpath("/html/body/div[3]/div[2]/div[2]/div/div[1]/div/div/span/text()").extract())
        salary_min, salary_max = parse_salary(response)
        company_nature = response.xpath("/html/body/div[3]/div[2]/div[4]/div[1]/div[2]/p[1]/text()").extract_first()
        industry = response.xpath("/html/body/div[3]/div[2]/div[4]/div[1]/div[2]/p[3]/@title").extract_first()
        job_description = response.xpath("/html/body/div[3]/div[2]/div[3]/div[1]/div")[0].xpath(
            "string(.)").extract_first().strip().replace("\xa0", "").split("\n")[0]
        info_data = infos.replace(
            "\xa0", "").split("|")
        location = list_get(info_data, 0, "")
        academic_requirements1 = list_get(info_data, 1, "")
        academic_requirements2 = list_get(info_data, 2, "")
        recruits_number = list_get(info_data, 3, "")
        release_date = list_get(info_data, 4, "")

        item = Scrapy51JobItem()
        item["job_name"] = job_name
        item["job_url"] = job_url
        item["company_name"] = company_name
        item["employee_welfare"] = employee_welfare
        item["salary_min"] = salary_min
        item["salary_max"] = salary_max
        item["company_nature"] = company_nature
        item["industry"] = industry
        item["job_description"] = job_description
        item["location"] = location
        item["academic_requirements"] = academic_requirements1 + academic_requirements2
        item["recruits_number"] = recruits_number.replace("招", "").replace("人", "")
        item["release_date"] = release_date
        yield item


def parse_salary(response):
    salary = response.xpath("/html/body/div[3]/div[2]/div[2]/div/div[1]/strong/text()").extract_first()
    if salary != "" and salary is not None:
        if salary.find(r"-") != -1:
            if salary.find(r"千") != -1:
                salary = re.findall(r"\d+", salary)
                return float(salary[0]), float(salary[1])
            elif salary.find(r"万/月") != -1:
                salary = re.findall(r"\d+", salary)
                return float(salary[0]) * 10, float(salary[1]) * 10
            elif salary.find(r"万/年") != -1:
                salary = re.findall(r"\d+", salary)
                return float(salary[0]) * 10 / 12, float(salary[1]) * 10 / 12
        else:
            if salary.find(r"千") != -1:
                salary = re.findall(r"\d+", salary)[0]
                return float(salary), float(salary)
            elif salary.find(r"万/月") != -1:
                salary = re.findall(r"\d+", salary)[0]
                return float(salary) * 10, float(salary) * 10
            elif salary.find(r"万/年") != -1:
                salary = re.findall(r"\d+", salary)[0]
                return float(salary) * 10 / 12, float(salary) * 10 / 12
            elif salary.find(r"万以上/年") != -1:
                salary = re.findall(r"\d+", salary)[0]
                return float(salary) * 10 / 12, float(salary) * 10 / 12
            else:  # 百元/月
                salary = re.findall(r"\d+", salary)[0]
                return float(salary) / 10, float(salary) / 10
    return 0, 0
